package com.xp.basic.heima.infofuture21;

import com.xp.basic.heima.nio.netty.simpledemo.chat.service.HelloService;
import com.xp.basic.heima.nio.netty.simpledemo.chat.service.HelloServiceImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyTest {

    public static void main(String[] args) {
        HelloService helloService = new HelloServiceImpl();
        MyInvokeHandler<HelloService> handler = new MyInvokeHandler<>(helloService);
        HelloService o = (HelloService) Proxy.newProxyInstance(HelloService.class.getClassLoader(), new Class<?>[]{HelloService.class}, handler);
        o.sayHello("dadas");
    }

    static class MyInvokeHandler<T> implements InvocationHandler{

        T  proxy;

        public MyInvokeHandler(T proxy) {
            this.proxy = proxy;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("---------------正在代理对象中执行方法----------");
            Object invoke = method.invoke(proxy, args);
            System.out.println("------------------ 执行代理方法之后 ----------------");

            return invoke;
        }
    }
}
